package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.PProductMapper;
import net.lcheng.model.PProduct;
import net.lcheng.model.PProductExample;
import net.lcheng.model.User;
import net.lcheng.service.PProductService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by zwr on 2016/8/30.
 */
@Service
public class PProductServiceImpl implements PProductService{
    @Resource
    private PProductMapper pProductMapper;
    @Override
    public PageList<PProduct> setPProductList(Integer pageIndex, Integer pageSize, String productName, Date start, Date edate) {
        PageHelper.startPage(pageIndex,pageSize);
        PProductExample example=new PProductExample();
        PProductExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(productName) == false) {
            criteria.andProductNameLike(productName);
        }
        if (start!=null && edate!=null){
            criteria.andCreateOnBetween(start,edate);
        }
        if(start != null&& edate==null ) {
            criteria.andCreateOnGreaterThanOrEqualTo(start);
        }
        if (edate!=null && start==null){
            criteria.andCreateOnLessThan(start);
        }
        example.or(criteria);
        example.setOrderByClause("product_id desc");
        List<PProduct> list=pProductMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        PageList pageList = new PageList<User>();
        pageList.list = pageInfo.getList();
        pageList.totalCount = pageInfo.getTotal();
        pageList.totalPage=pageInfo.getPages();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        return pageList;
    }

    @Override
    public int dateStatus(String productId) {
        return pProductMapper.deleteByPrimaryKey(productId);
    }
    @Override
    public int getPProductByStatus(String id, Integer status) {
        PProduct pProduct = new PProduct();
        pProduct.setProductId(id);
        pProduct.setStatus(status);
        return pProductMapper.updateByPrimaryKeySelective(pProduct);
    }

    @Override
    public int addPProduct(PProduct pProduct) {
        return pProductMapper.insertSelective(pProduct);
    }

    @Override
    public List<PProduct> getPProductByStatus(Integer status) {
        PProductExample example=new PProductExample();
        example.or().andStatusEqualTo(status);
        return pProductMapper.selectByExample(example);
    }

    @Override
    public PProduct getProductByPrimaryKey(String productId) {
        return pProductMapper.selectByPrimaryKey(productId);
    }

}
